package skinfactory
{
	import events.ButtonEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class ImageButton extends Sprite
	{
		private var _normalImg:Bitmap;
		private var _overImg:Bitmap;
		private var _downImg:Bitmap; 
		
		public function ImageButton(normalImg:Bitmap,overImg:Bitmap=null,downImg:Bitmap=null)
		{
			_normalImg = normalImg;
			_overImg = overImg;
			_downImg = downImg;			
			addChild(_normalImg);
			if(overImg != null) addChild(_overImg);
			if(downImg != null) addChild(_downImg);
			setChildIndex(_normalImg,numChildren-1);
			init();
		}

		private function init():void {
			buttonMode = true;			
			addEventListener(MouseEvent.CLICK, onMouseClickListener );
			addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownListener);
			addEventListener(MouseEvent.MOUSE_OVER,onMouseOverListener);
			addEventListener(MouseEvent.MOUSE_OUT,onMouseOutListener);	
			addEventListener(MouseEvent.MOUSE_UP, onMouseUpListener );		
		}		
		
		private function onMouseClickListener( e: MouseEvent ): void
		{
			this.dispatchEvent( new ButtonEvent( ButtonEvent.CLICK ) ); 
		}
		private function onMouseOverListener(e:MouseEvent):void {
			//trace("ImageButton:onMouseOverListener");
			if(_overImg != null) {
				//trace("ImageButton :: onMouseOverListener");
				setChildIndex(_overImg,numChildren-1);
			}
		}
		private function onMouseOutListener(e:MouseEvent):void {	
			//trace("ImageButton:onMouseOutListener");	
			if(_normalImg != null) {	
				//trace("ImageButton :: onMouseOutListener");
				setChildIndex(_normalImg,numChildren-1);
			}
		}
		private function onMouseDownListener(e:MouseEvent):void {	
			//trace("ImageButton:onMouseDownListener");			
			if(_downImg != null) {
				//trace("ImageButton :: onMouseDownListener");				
				setChildIndex(_downImg,numChildren-1);			
			}
		}	
		private function onMouseUpListener( e: MouseEvent ): void
		{
			if(_overImg != null) {
				//trace("ImageButton :: onMouseDownListener");				
				setChildIndex(_overImg,numChildren-1);			
			}
		}
		public function getImage( status: String ): Bitmap
		{
			var rtn: Bitmap;
			
			var bitmapData: BitmapData;
			switch( status )
			{
				case "Normal":
					bitmapData = _normalImg.bitmapData.clone();
					break;
				case "Over":
					if( _overImg != null )
						bitmapData = _overImg.bitmapData.clone();
					break;
				case "Down":
					if( _downImg != null )
						bitmapData = _downImg.bitmapData.clone();
					break;
			}
			rtn = new Bitmap( bitmapData );
			return rtn
		}	
	}
}